import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CaveWorld here.
 * 
 * @author Manuel Nava
 * @version 1.0
 */
public class CaveWorld extends World
{

    private Player player;
    private Life life;
    private boolean move;
    private int lifePoints;
    private int level;
    private int contMov;
    private int score;
    private Square square;
    private int time;
    
    /**
     * Constructor for objects of class CaveWorld.
     * 
     */
    public CaveWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1,false); 
        lifePoints=4;
        move=true;
        level=0;        
        level0();
        contMov=0;
        score=0;
        time=0;
    }
    
    /**
     * Increments the time variable.
     */
    public void act()
    {
        time++;
    }
    
    /**
     * Sets the objects of the menu and add these to the world.
     */
    public void level0()
    {
        super.removeObjects(getObjects(null));
        setBackground("menuScreen.png");
        addObject(new Jugar(),155,165);
        addObject(new Ayuda(),472,210);
        addObject(new Creditos(),396,376);
        addObject(new Records(),200,294);
    }
    
    /**
     * Show the records adding the scoreboard class object.
     */
    public void records()
    {
        super.removeObjects(getObjects(null));
        addObject(new ScoreBoard(getWidth(),getHeight()-100),300,200);
        addObject(new Menu(),299,368);
    }
    
    /**
     * Setter for the movement counter.
     */
    public void setCont(int d)
    {
        contMov=d;
    }

    /**
     * return the actual level.
     */
    public int getLevel()
    {
        return level;
    }
    
    /**
     * shows the credits of the game.
     */
    public void creditos()
    {
        super.removeObjects(getObjects(null));
        setBackground("creditScreen.png");
        addObject(new Menu(),299,368);
    }
    
    /**
     * Shows the help screen.
     */
    public void ayuda()
    {
        super.removeObjects(getObjects(null));
        setBackground("Scores.png");                     //en realidad no son scores, es la ayuda.
        addObject(new Menu(),299,368);
    }
    
    /**
     * Getter for the score the Player has.
     */
    public int getScore()
    {
        return score;
    }
    
    /**
     * Setter for the level variable.
     */
    public void setLevel(int d)
    {
        level=d;
    }
    
    /**
     * Getter for the square variable. Returns a Square class object.
     */
    public Square getSquare()
    {
        return square;
    }
    
    /**
     * Setter for the Player variable powerUp.
     */
    public void powerUp(int d)
    {
        player.setPowerUp(d);
    }

    /**
     * Removes and axe class given from the world.
     */
    public void deleteAxe(Axe anAxe)
    {
        removeObject(anAxe);
        }

    /**
     * Getter for the move variable. return a boolean.
     */
    public boolean getMove()
    {
        return move;
    }
    
    /**
     * Getter for the movemento counter. Returns an integer.
     */
    public int getCont()
    {
        return contMov;
    }
    
    /**
     * Deletes all the objects the world has, and checks the lifes left, it it has mora than one life the level will reset, else the scoreboard will be shown and the level0 method
     * is called.
     */
    public void lostLife()
    {
        super.removeObjects(getObjects(null));
        lifePoints--;
        if(score-5000>0)
           score=score-5000;
        else score=0;
        if(lifePoints>1)
        {
          if(level==1)
           level1();
          if(level==2)
           level2();
          if(level==3)
           level3();
        }else 
        {
            ScoreBoard scoreboard = new ScoreBoard(getWidth(),getHeight());
            addObject(scoreboard,300,200);
            scoreboard.storeScore(score);
            score=0;
            Greenfoot.delay(100);
            level0();
            level=0;
        }
           
    }
    
    /**
     * Creates the objects to be added to the world at level 1.
     */
    public void level1()
    {
        super.removeObjects(getObjects(null));
        setBackground("Forest.png");
        contMov=0;
        addObject(new Tree(),300,200);
        addObject(new Tree(),900,200);
        addObject(new Tree(),1500,200);
        addObject(new Tree(),2100,200);
        addObject(new Tree(),2700,200);
        addObject(new Ground(), 103, 387);
        addObject(new Ground(), 300, 387);
        addObject(new Ground(), 499, 387);
        addObject(new Floor(),400,399);
        addObject(new Floor(),1200,399);
        addObject(new Floor(),2000,399);
        addObject(new Floor(),2800,399);
        addObject(new Grass(),3600,386);
        addObject(square = new Square(),500,50);
        addObject(new Score(score),300,50);
        player = new Player();
        addObject(player, 136, 300);
        addObject(new Snail(), 450, 331);
        addObject(life= new Life(lifePoints),108,37);
        addObject(new Campfire(),550,331);
        addObject(new Strawberry(),700,300);
        addObject(new Snail(),850,331);
        addObject(new Snail(),1050,331);
        addObject(new Snail(),1900,331);
        addObject(new Snail(),2450,331);
        addObject(new Snail(),3000,331);
        addObject(new Campfire(),1450,331);
        addObject(new Campfire(),1500,331);
        addObject(new Campfire(),2250,331);
        addObject(new Strawberry(),1750,300);
    }
    
    /**
     * Setter of the lifePoints variable.
     */
    public void setLifePoints(int d)
    {
        lifePoints=d;
    }
    
    /**
     * Method that decrements the lifepoints the player has.
     */
    public void DecLifePoints()
    {
        lifePoints--;
    }
    
    /**
     * Deletes all the object of the level 1 and calls the method level2. Also calculates the points the Player has won.
     */
    public void finishLevel1()
    {
        super.removeObjects(getObjects(null));
        score=lifePoints*3000-time/10;
        time=0;
        level=2;
        level2();
    }
    
    /**
     * This method deletes all the objects of the level 3 and sets all the final scores the player has got, also calls the scoreboard moethod storeScore, and shows the scoreboard.
     * 
     */
    public void win()
    {
        super.removeObjects(getObjects(null));
        score+=lifePoints*3000-time/10;
        time=0;
        setBackground("finalScreen.png");
        addObject(new Score(score),400,50);
        Greenfoot.delay(1000);
        ScoreBoard scoreboard;
        addObject(scoreboard=new ScoreBoard(getWidth(),getHeight()),300,200);
        scoreboard.storeScore(score);
        score=0;
        Greenfoot.delay(1000);
        level=0;
        level0();
    }
    
    /**
     * Deletes all the object of the level 2 and calls the method level3. Also calculates the points the Player has won.
     */
    public void finishLevel2()
    {
        super.removeObjects(getObjects(null));
        score+=lifePoints*3000-time/10;
        time=0;
        level=3;
        level3();
    }
    
    /**
     * Creates the level 2 adding objects in certains positions.
     */
    public void level2()
    {
        contMov=0;
        setBackground(new GreenfootImage("beach.png"));
        addObject(new Ground(), 103, 387);
        addObject(new Ground(), 300, 387);
        addObject(new Ground(), 499, 387);
        addObject(new Palm(),0,200);
        addObject(new Palm(),600,200);
        addObject(new Palm(),1200,200);
        addObject(new Palm(),1800,200);
        addObject(new Palm(),2400,200);
        addObject(new Palm(),3200,200);
        addObject(new Grass(),400,386);
        addObject(new Grass(),800,386);
        addObject(new Grass(),1600,386);
        addObject(new Grass(),2400,386);
        addObject(new Grass(),3200,386);
        addObject(square = new Square(),500,50);
        addObject(new Score(score),300,50);
        addObject(new Cave(),3500,150);
        addObject(player = new Player(),136,300);
        addObject(life=new Life(lifePoints),108,37);
        ////////////   Enemys&&Traps   ////////////
        addObject(new Snail(),400,331);
        addObject(new Snail(),1350,331);
        addObject(new Snail(),1550,331);
        addObject(new Campfire(),850,331);
        addObject(new Campfire(),950,331);
        addObject(new Campfire(),1050,331);
        addObject(new Snake(),1950,315);
        addObject(new Snake(),2300,315);
        addObject(new Frog(),2500,331);
        addObject(new Frog(),3100,331);
        addObject(new Cloud(),930,277);
        addObject(new Cherry(),1200,300);
        
        
    }
    
    public void level3()
    {
        contMov=0;
        setBackground("cave.png");
        addObject(new Ground(), 103, 387);
        addObject(new Ground(), 300, 387);
        addObject(new Ground(), 499, 387);
        addObject(new CaveCeiling(),400,30);
        addObject(new CaveCeiling(),1200,30);
        addObject(new CaveCeiling(),2000,30);
        addObject(new CaveCeiling(),2800,30);
        addObject(new CaveCeiling(),3600,30);
        addObject(new CaveFloor(),400,399);
        addObject(new CaveFloor(),1200,399);
        addObject(new CaveFloor(),2000,399);
        addObject(new CaveFloor(),2800,399);
        addObject(new CaveFloor(),3600,399);
        addObject(new Cloud(),1950,300);
        addObject(square = new Square(),500,50);
        addObject(new Score(score),300,50);
        addObject(player= new Player(),136,300);
        addObject(new Cherry(),1400,350);
        addObject(new Strawberry(),750,200);
        addObject(life= new Life(lifePoints),108,37);
        //////////Enemys & Traps////////////////
        addObject(new Snail(),500,331);
        addObject(new Snail(),1100,331);
        addObject(new Snail(),1750,331);
        addObject(new Snake(),1600,318);
        addObject(new Frog(),1950,331);
        addObject(new Frog(),2200,331);
        addObject(new Campfire(),300,331);
        addObject(new Campfire(),1200,331);
        addObject(new Campfire(),1950,331);
        addObject(new Rock(),1100,50);
        addObject(new Rock(),1950,50);
        addObject(new Stalactite(),1350,50);
        addObject(new Stalactite(),2300,50);
        addObject(new Stalactite(),1800,50);
        addObject(new Boss(),3000,277);
        addObject(new Tina(),3300,319);
        
        
    }
}
